import { applyActivity, getTemplateList  } from "../../../api/scene";
import { timeoutTask } from "../../../utils/index";

const app = getApp();
let btnLock = false;
const SCENE = [
  {
    value: "0",
    name: "婚礼",
  },
  {
    value: "91",
    name: "中式婚礼版",
  },
  {
    value: "1",
    name: "年会",
  },
  {
    value: "2",
    name: "生日(通用)",
  },
  {
    value: "21",
    name: "宝宝宴",
  },
  {
    value: "22",
    name: "寿宴",
  },
  {
    value: "23",
    name: "成人礼",
  },
  {
    value: "24",
    name: "百露宴",
  },
  {
    value: "25",
    name: "满月宴",
  },
  {
    value: "26",
    name: "圆锁宴",
  },
  {
    value: "41",
    name: "毕业典礼",
  },
  {
    name: '谢师宴',
    value: '42',
  },
  {
    name: '金榜题名',
    value: '43',
  },
  {
    name: '同学会',
    value: '51',
  },
  {
    name: '乔迁宴',
    value: '52',
  },
  {
    name: '会销',
    value: '53',
  },
  {
    name: '订婚宴',
    value: '54',
  }
];
const scheduleList = [
  {
    id: '1',
    label: '午宴'
  },
  {
    id: '2',
    label: '晚宴'
  },
];
Page({
  data: {
    activityName: "",
    activityDate: "",
    activityAddress: "",
    activeSchedule: "1",
    sceneList: SCENE,
    scheduleList: scheduleList,
    startDate: "",
    activeSceneVal: "0",
    templateList: [],
    is_gift_to_hbkd: '0',
    is_help_hm: '0',
    hby_kd_rate_val: '',
    is_close_coin: '0',
    emcee_name: '',
    isSendSystemGift: '',
    isNeedUserComment: '',
    newcomer_info: {
      groom_name: '',
      groom_phone: '',
      bride_name: '',
      bride_phone: ''
    }
  },
  onLoad() {
    let currentDateStr = this.getCurrentDate();
    btnLock = false;
    this.setData({
      startDate: currentDateStr,
    });
    this.setData({
      emcee_name: wx.getStorageSync('user_name'),
      isSendSystemGift: wx.getStorageSync('isSendSystemGift'),
      isNeedUserComment: wx.getStorageSync('isNeedUserComment')
    })

    //调用活动模板列表方法
    this.getTemplateList()
    console.log("data:", this.data);
  },
  onShow() {
    let pages = getCurrentPages();//当前页面栈
    let prevPage = pages[pages.length - 2];//上一页面
    
  },
  activityNameBlur(e) {
     
    this.setData({
      activityName: e.detail.value,
    });
  },
  bindDateChange(e) {
     
    this.setData({
      activityDate: e.detail.value,
    });
  },
  chooseScene(e) {
     
    this.setData({
      activeSceneVal: e.currentTarget.dataset.scene,
    });
  },
  //选择活动时段
  chooseSchedule(e) {
    this.setData({
      activeSchedule: e.currentTarget.dataset.schedule,
    });
  },
  activityAddressBlur(e) {
     
    this.setData({
      activityAddress: e.detail.value,
    });
  },
  groomNameBlur(e) {
    this.setData({
      'newcomer_info.groom_name': e.detail.value
    });
  },
  groomPhoneBlur(e) {
    this.setData({
      'newcomer_info.groom_phone': e.detail.value
    });
  },
  brideNameBlur(e) {
    this.setData({
      'newcomer_info.bride_name': e.detail.value
    });
  },
  bridePhoneBlur(e) {
    this.setData({
      'newcomer_info.bride_phone': e.detail.value
    });
  },
  //获取活动模板列表
  getTemplateList() {
    this.setData({
      templateList: []
    })
    getTemplateList({
      showCount: 10,
      currentPage: 1
    }).then((res) => {
      console.log("获取活动模板列表res:", res);
      this.setData({
        templateList: res.data.liveList.map((item) => ({
          id: item.id,
          label: item.theme,
        }))
      });
      if (res.data.endSpl) {
        //is_gift_to_hbkd_real
        if (res.data.endSpl.is_gift_to_hbkd === '0') {
          this.setData({
            is_gift_to_hbkd: '0'
          })
        } else {
          this.setData({
            is_gift_to_hbkd: res.data.endSpl.is_gift_to_hbkd
          })
        }
        //hby_kd_rate_val
        if (res.data.endSpl.hby_kd_rate_val) {
          this.setData({
            hby_kd_rate_val: res.data.endSpl.hby_kd_rate_val.toString()
          })
        }
        //is_close_coin
        if (res.data.endSpl.is_close_coin && res.data.endSpl.is_close_coin === '1') {
          this.setData({
            is_close_coin: '1'
          })
        }
        //is_help_hm
        if (res.data.endSpl.is_help_hm && res.data.endSpl.is_help_hm === '1') {
          
          this.setData({
            is_help_hm: '1'
          })
        }
      }
    })
    .catch((err) => {
      console.log(err);
    });
  },
  applyActivity() {
    if (!this.data.activityName) {
      wx.showToast({
        title: "请输入活动名称",
        icon: "none",
      });
      return;
    }
    if (!this.data.activityDate) {
      wx.showToast({
        title: "请选择活动日期",
        icon: "none",
      });
      return;
    }
    if (!this.data.activityAddress) {
      wx.showToast({
        title: "请输入活动地点",
        icon: "none",
      });
      return;
    }

    const { activeSceneVal, newcomer_info } = this.data;
    
    if (activeSceneVal === '0') {
      if (newcomer_info.groom_phone && newcomer_info.groom_phone.length !== 11) {
        wx.showToast({ title: '新郎手机号格式错误', icon: 'none' });
        return;
      }
      if (newcomer_info.bride_phone && newcomer_info.bride_phone.length !== 11) {
        wx.showToast({ title: '新娘手机号格式错误', icon: 'none' });
        return;
      }
    }

    if (btnLock) {
      return;
    }
    btnLock = true;
    const params = {
      activityAddress: this.data.activityAddress,
      activeSceneVal: this.data.activeSceneVal,
      activityName: this.data.activityName,
      activityDate: this.data.activityDate,
      emcee_name: this.data.emcee_name,
      isSendSystemGift: this.data.isSendSystemGift,
      isNeedUserComment: this.data.isNeedUserComment,
      is_help_hm: this.data.is_help_hm,
      wedding_schedule: this.data.activeSchedule,
      is_gift_to_hbkd: this.data.is_gift_to_hbkd,
      hby_kd_rate_val: this.data.hby_kd_rate_val,
      is_close_coin: this.data.is_close_coin,
      dy_appid: '',
    };
   if (activeSceneVal === '0' && (newcomer_info.groom_name || newcomer_info.groom_phone || newcomer_info.bride_name || newcomer_info.bride_phone)) {
       params.newcomer_json = JSON.stringify(newcomer_info);
     }
    applyActivity(params)
      .then((res) => {
        btnLock = false;
        wx.showToast({
          title: "活动申请成功",
          icon: "none",
        });
        //清空表单数据
        this.setData({
          activityAddress: '',
          activeSceneVal: '0',
          activityName: '',
          activityDate: '',
          activeSchedule: '1',
          newcomer_info: {
            groom_name: '',
            groom_phone: '',
            bride_name: '',
            bride_phone: ''
          }
        })
        // 通知首页返回后需要刷新
        app.globalData.homeNeedRefresh = true;
        timeoutTask(() => {
          // wx.navigateBack();
          wx.switchTab({ url: '/pages/index/index'})
        }, 500);
      })
      .catch((err) => {
        btnLock = false;
          
      });
  },
  getCurrentDate() {
    let time = new Date();
    let y = time.getFullYear();
    let m =
      time.getMonth() + 1 < 10
        ? "0" + (time.getMonth() + 1)
        : time.getMonth() + 1;
    let d = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
    return y + "-" + m + "-" + d;
  },
});
